//
//  KABMallShopCartTCell.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/20.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABMallShopCartTCell.h"
#import "GlobalHeader.h"
#import "UIColor+XDHex.h"
#import "KABModel.h"
#import <YYWebImage.h>

@implementation KABMallShopCartTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.cntBgView.layer.borderWidth = 0.5;
    self.cntBgView.layer.borderColor = [UIColor xd_colorWithHex:KABGlobalColorBtnBorderColor].CGColor;
    self.cntTextField.layer.borderWidth = 0.5;
    self.cntTextField.layer.borderColor = [UIColor xd_colorWithHex:KABGlobalColorBtnBorderColor].CGColor;
    
    [self.selBtn addTarget:self action:@selector(selBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.editBtn addTarget:self action:@selector(editBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.decreaseBtn addTarget:self action:@selector(decreaseBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.increaseBtn addTarget:self action:@selector(increaseBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)selBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABMallShopCartTCell:selBtnTapped:)]) {
        [self.delegate kABMallShopCartTCell:self selBtnTapped:btn];
    }
}

- (void)editBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABMallShopCartTCell:editBtnTapped:)]) {
        [self.delegate kABMallShopCartTCell:self editBtnTapped:btn];
    }
}

- (void)decreaseBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABMallShopCartTCell:decreaseBtnTapped:)]) {
        [self.delegate kABMallShopCartTCell:self decreaseBtnTapped:btn];
    }
}

- (void)increaseBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABMallShopCartTCell:increaseBtnTapped:)]) {
        [self.delegate kABMallShopCartTCell:self increaseBtnTapped:btn];
    }
}

- (void)configViewWithProDetail:(KABMallProDetail *)product couldSel:(BOOL)couldSel isEdit:(BOOL)isEdit {
    if (couldSel) {
        self.selBtn.hidden = NO;
        self.selBtnWidthConstraint.constant = 30.0;
    } else {
        self.selBtn.hidden = YES;
        self.selBtnWidthConstraint.constant = 0;
    }
    
    self.selBtn.selected = product.isSelected;
    
    [self.productImgView yy_setImageWithURL:[NSURL URLWithString:product.product_cover_image_url] placeholder:[KABDefaultVarModel sharedInstance].bigDefaultImg options:YYWebImageOptionSetImageWithFadeAnimation completion:nil];
    
    self.nameLabel.text = product.product_name;
    self.propertyLabel.text = product.selPropertyStr;
    self.editBtn.hidden = !isEdit;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%.2f", product.selOriginalPrice / 100.0];
    self.cntLabel.text = [NSString stringWithFormat:@"x%zd", product.number];
    
    self.cntBgView.hidden = !isEdit;
    self.cntTextField.text = [NSString stringWithFormat:@"%zd", product.number];
}

@end


@implementation KABMallShopCartTCell (MallOrderList)
- (void)configViewWithOrderProItem:(KABMallOrdProductItem *)product couldSel:(BOOL)couldSel isEdit:(BOOL)isEdit {
    if (couldSel) {
        self.selBtn.hidden = NO;
        self.selBtnWidthConstraint.constant = 30.0;
    } else {
        self.selBtn.hidden = YES;
        self.selBtnWidthConstraint.constant = 0;
    }
    [self.productImgView yy_setImageWithURL:[NSURL URLWithString:product.product_cover_image_url] placeholder:[KABDefaultVarModel sharedInstance].bigDefaultImg options:YYWebImageOptionSetImageWithFadeAnimation completion:nil];
    
    self.nameLabel.text = product.product_name;
    self.propertyLabel.text = product.product_property;
    self.editBtn.hidden = !isEdit;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%.2f", product.original_price / 100.0];
    self.cntLabel.text = [NSString stringWithFormat:@"x%zd", product.number];
    
    self.cntBgView.hidden = !isEdit;
    self.cntTextField.text = [NSString stringWithFormat:@"%zd", product.number];
}

@end
